﻿using Microsoft.Xna.Framework;

namespace Minigolf
{
    public static class Camera
    {
        private static Vector2 Position = Vector2.Zero;
        private static Vector2 Velocity = Vector2.Zero;
        private static Vector2 Offset = Vector2.Zero;

        private static float Friction = 1f;
        private static Rectangle ClampView = new Rectangle(0, 0, 32, 32);
        private static float Zoom = 32f;

        public static void Update(GameTime gameTime)
        {
            Velocity *= Friction;
            Position += (Velocity * (float)gameTime.ElapsedGameTime.TotalSeconds);

            Position.X = MathHelper.Clamp(Position.X, ClampView.X, ClampView.Width - (Game1.ScrW() / Zoom) + (Offset.X));
            Position.Y = MathHelper.Clamp(Position.Y, ClampView.Y, ClampView.Height - (Game1.ScrH() / Zoom) + (Offset.Y));
        }

        public static void SetViewClamp(Rectangle Rect) { ClampView = Rect; }
        public static void SetViewClamp(int x, int y, int w, int h) { ClampView = new Rectangle(x, y, w, h); }

        public static void SetPos(float x, float y) { Position = new Vector2(x, y); }
        public static void SetVelocity(float x, float y) { Velocity = new Vector2(x, y); }
        public static void SetOffset(float x, float y) { Offset = new Vector2(x, y); }

        public static void SetVelocity(Vector2 Vel) { Velocity = Vel; }
        public static void SetPos(Vector2 Pos) { Position = Pos; }

        public static void SetZoom(float zoom) { Zoom = zoom; }

        public static int GetX() { return (int)Position.X; }
        public static int GetY() { return (int)Position.Y; }

        public static float GetZoom() { return Zoom; }
    
        public static Vector2 GetPos()
        {
            Position.X = MathHelper.Clamp(Position.X, ClampView.X, ClampView.Width - (Game1.ScrW() / Zoom) + (Offset.X / Zoom));
            Position.Y = MathHelper.Clamp(Position.Y, ClampView.Y, ClampView.Height - (Game1.ScrH() / Zoom) + (Offset.Y / Zoom));

            return Position; 
        }

        public static Vector2 GetVel() { return Velocity; }

        public static Rectangle GetViewClamp() { return ClampView; }

        public static Vector2 DrawPos(Vector2 pos)
        {
            return CakeToPixel(pos - GetPos()) + CakeToPixel(Offset);
        }

        public static Vector2 WorldToCoord(Vector2 pos)
        {
            Vector2 Pos = PixelToCake(pos);

            return (Pos + GetPos()) - Offset;
        }

        public static float WorldToCoordX(float X) { return PixelToCake(X + GetPos().X - Offset.X); }
        public static float WorldToCoordY(float Y) { return PixelToCake(Y + GetPos().Y - Offset.Y); }

        public static float PixelToCake(float f) { return f / GetZoom(); }
        public static Vector2 PixelToCake(Vector2 Pos) { return Pos / GetZoom(); }

        public static float CakeToPixel(float f) { return f * GetZoom(); }
        public static Vector2 CakeToPixel(Vector2 Pos) { return Pos * GetZoom(); }
    }
}
